package cn.jinjimi.app.main.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.app.AccountManager;
import cn.jinjimi.app.R;
import cn.jinjimi.domain.data.net.ApiConstants;
import cn.jinjimi.app.controller.event.SelectPagerEvent;
import cn.jinjimi.app.entity.TaskListBean;
import cn.jinjimi.framework.router.Router;
import cn.jinjimi.app.bean.RouterConstants;
import cn.jinjimi.app.main.activity.WebActivity;
import cn.jinjimi.app.main.adapter.extend.MyBaseAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Jinsen on 2015/6/2.
 */
public class TaskListAdapter extends MyBaseAdapter<TaskListBean.TaskBean> {

    Context mContext = null;

    private int mExpDescPicWidth = 0;

    List<Integer> mExpIndexList = new ArrayList<Integer>();

    public TaskListAdapter(Context context) {
        mContext = context;

        mExpDescPicWidth = (int) (mContext.getResources().getDisplayMetrics().widthPixels - 40 * mContext.getResources().getDisplayMetrics().density);
    }

    @Override
    public void setItems(List<TaskListBean.TaskBean> items) {
        super.setItems(items);
        mExpIndexList.clear();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        if (convertView == null) {
            holder = new Holder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.task_list_item_layout, null);

            holder.icon = (ImageView) convertView.findViewById(R.id.task_item_icon_imageView);
            holder.title = (TextView) convertView.findViewById(R.id.task_item_title_textView);

            holder.progressView[0] = (ImageView) convertView.findViewById(R.id.task_item_progress_1_imageView);
            holder.progressView[1] = (ImageView) convertView.findViewById(R.id.task_item_progress_2_imageView);
            holder.progressView[2] = (ImageView) convertView.findViewById(R.id.task_item_progress_3_imageView);
            holder.progressView[3] = (ImageView) convertView.findViewById(R.id.task_item_progress_4_imageView);
            holder.progressView[4] = (ImageView) convertView.findViewById(R.id.task_item_progress_5_imageView);

            holder.taskProgress = (TextView) convertView.findViewById(R.id.task_item_progress_textView);
            holder.taskCount = (TextView) convertView.findViewById(R.id.task_item_count_textView);
            holder.expStatus = (TextView) convertView.findViewById(R.id.task_item_exp_button);

            holder.expLayout = convertView.findViewById(R.id.task_item_exp_content_linearLayout);
            holder.expContent = (TextView) convertView.findViewById(R.id.task_item_exp_content_textView);
            holder.expDesc = (ImageView) convertView.findViewById(R.id.task_item_exp_desc_imageView);

            holder.action = (TextView) convertView.findViewById(R.id.task_item_action_textView);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        TaskListBean.TaskBean task = getItem(position);

        holder.icon.setImageURI(Uri.parse(task.getTask_icon_url()));
        holder.title.setText(task.getName());
        holder.action.setText(task.getAction_name());
        holder.action.setOnClickListener(new ActionOnClickListener(task.getAction_type()));
        setTaskProgress(holder, task);
        setExpContent(holder, task, position);

        return convertView;
    }

    private void setTaskProgress(Holder holder, TaskListBean.TaskBean data) {
        int count = data.getAction_count();
        int progress = data.getAction_record_count();

        holder.taskProgress.setText(String.valueOf(progress));
        holder.taskCount.setText(String.valueOf(count));

        for (int i = 0; i < 5; i++) {
            ImageView iv = holder.progressView[i];
            if (i < count) {
                iv.setVisibility(View.VISIBLE);
                if (i < progress) {
                    iv.setSelected(true);
                } else {
                    iv.setSelected(false);
                }
            } else {
                iv.setVisibility(View.GONE);
            }
        }
    }

    private void setExpContent(Holder holder, TaskListBean.TaskBean data, int position) {
        TaskListBean.TipsBean tips = data.getTips();
        if (tips == null) {
            holder.expStatus.setOnClickListener(null);
            holder.expStatus.setVisibility(View.GONE);
            holder.expLayout.setVisibility(View.GONE);
        } else {
            holder.expStatus.setOnClickListener(new ExpStatusOnClickListener(holder, position));
            holder.expStatus.setVisibility(View.VISIBLE);
            if (mExpIndexList.contains(Integer.valueOf(position))) {
                holder.expLayout.setVisibility(View.VISIBLE);
                holder.expStatus.setSelected(true);
                setExpDescImage(holder.expDesc, tips.getImage_url());
            } else {
                holder.expLayout.setVisibility(View.GONE);
                holder.expStatus.setSelected(false);
            }
            if (StringUtils.isNotEmpty(tips.getContent())) {
                holder.expContent.setText(tips.getContent());
                holder.expContent.setVisibility(View.VISIBLE);
            } else {
                holder.expContent.setVisibility(View.GONE);
            }
        }
    }

    private void setExpDescImage(ImageView iv, String imageUrl) {
        if (StringUtils.isEmpty(imageUrl))
            iv.setVisibility(View.GONE);
        else {

            if (imageUrl.contains("imagesize_")) {
                String iszs = imageUrl.substring(imageUrl.indexOf("imagesize_") + 10, imageUrl.lastIndexOf("."));
                String iw = iszs.substring(0, iszs.indexOf("x"));
                String ih = iszs.substring(iszs.indexOf("x") + 1);
                iv.getLayoutParams().height = (int) (((float) mExpDescPicWidth / Float.valueOf(iw)) * Float.valueOf(ih));
            }
            iv.setImageURI(Uri.parse(imageUrl));
        }
    }

    class Holder {
        public ImageView icon;
        public TextView title;
        public ImageView[] progressView = new ImageView[5];
        public TextView taskProgress;
        public TextView taskCount;
        public TextView expStatus;
        public View expLayout;
        public TextView expContent;
        public ImageView expDesc;
        public TextView action;
    }

    class ExpStatusOnClickListener implements View.OnClickListener {
        Holder mHolder = null;
        int mPosition = 0;

        public ExpStatusOnClickListener(Holder holder, int position) {
            this.mHolder = holder;
            mPosition = position;
        }

        @Override
        public void onClick(View v) {
            if (mExpIndexList.contains(Integer.valueOf(mPosition))) {
                mExpIndexList.remove(Integer.valueOf(mPosition));
                mHolder.expLayout.setVisibility(View.GONE);
                mHolder.expStatus.setSelected(false);
            } else {
                mHolder.expLayout.setVisibility(View.VISIBLE);
                mExpIndexList.add(Integer.valueOf(mPosition));
                if (StringUtils.isNotEmpty(getItem(mPosition).getTips().getContent())) {
                    mHolder.expContent.setText(getItem(mPosition).getTips().getContent());
                    mHolder.expContent.setVisibility(View.VISIBLE);
                } else {
                    mHolder.expContent.setVisibility(View.GONE);
                }
                setExpDescImage(mHolder.expDesc, getItem(mPosition).getTips().getImage_url());
                mHolder.expStatus.setSelected(true);
            }
        }
    }

    class ActionOnClickListener implements View.OnClickListener {
        int mActionType = 0;

        public ActionOnClickListener(int actionType) {
            mActionType = actionType;
        }

        @Override
        public void onClick(View v) {

        }
    }
}
